package com.tensin.mall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tensin.common.utils.R;
import com.tensin.mall.ware.feign.ProductFeignService;
import com.tensin.mall.ware.vo.SkuHasStockTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tensin.common.utils.PageUtils;
import com.tensin.common.utils.Query;

import com.tensin.mall.ware.dao.WareSkuDao;
import com.tensin.mall.ware.entity.WareSkuEntity;
import com.tensin.mall.ware.service.WareSkuService;
import org.springframework.util.StringUtils;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    WareSkuDao wareSkuDao;

    @Autowired
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<WareSkuEntity> wrapper = new LambdaQueryWrapper<>();

        String skuId = (String) params.get("skuId");
        if (StringUtils.hasText(skuId) && !"0".equalsIgnoreCase(skuId)){
            wrapper.eq(WareSkuEntity::getSkuId,skuId);
        }



        String wareId = (String) params.get("wareId");
        if (StringUtils.hasText(wareId) && !"0".equalsIgnoreCase(wareId)){
            wrapper.eq(WareSkuEntity::getWareId,wareId);
        }


        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    //将成功采购的进行入库
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //1 判断如果没有库存记录，则为新增操作
        List<WareSkuEntity> entities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if(entities == null || entities.size() == 0){
            WareSkuEntity skuEntity = new WareSkuEntity();
            skuEntity.setSkuId(skuId);
            skuEntity.setStock(skuNum);
            skuEntity.setWareId(wareId);
            skuEntity.setStockLocked(0);
            //TODO 远程查询sku的名字 如果失败，事务无需回滚（因为直接被catch了）
            //1以上是第一种处理事务不回滚的方法
            //TODO 其他处理事务不回滚的方法，高级篇解决
            try {
                R info = productFeignService.info(skuId);
                Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");

                if(info.getCode() == 0){
                    skuEntity.setSkuName((String) data.get("skuName"));
                }
            }catch (Exception e){

            }


            wareSkuDao.insert(skuEntity);
        }else{
            wareSkuDao.addStock(skuId,wareId,skuNum);
        }

    }


    //查询sku是否还有库存
    @Override
    public List<SkuHasStockTo> getSkuHasStock(List<Long> skuIds) {

        List<SkuHasStockTo> collect = skuIds.stream().map(skuId -> {
            SkuHasStockTo skuHasStockTo = new SkuHasStockTo();

            Long count = baseMapper.getSkuStock(skuId);

            skuHasStockTo.setId(skuId);
            skuHasStockTo.setHasStock(count == null ? false : count > 0);
            return skuHasStockTo;
        }).collect(Collectors.toList());

        return collect;
    }

}